package xin.infinite.service;

import org.apache.commons.lang.StringUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.DigestUtils;
import xin.infinite.dao.UserDao;
import xin.infinite.entity.User;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.UUID;


@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;


    @Reference(check = false)
    private CodeService codeService;

    public User saveUser(User user) {
        return userDao.save(user);
    }

    public User findUser(String email, String password) {
        if(StringUtils.isEmpty(email)||StringUtils.isEmpty(password)){
            return null;
        }
        User user=new User();
        user.setEmail(email);
        user.setPassword(password);
        List<User> all = userDao.findAll(Example.of(user));
        if(CollectionUtils.isEmpty(all)){
            return null;
        }else {
            return all.get(0);
        }
    }

    @Override
    public boolean register(String email, String password, String code) {
        if(StringUtils.isBlank(email)||StringUtils.isBlank(password)||StringUtils.isBlank(code)){
            return false;
        }
        if(isRegister(email)){
            System.out.println("该邮箱已被注册："+email);
            return false;
        }
        //验证码是否正确
        int codeCorrect = codeService.isCorrect(email, code);
        System.out.println("密码是："+password);
        if(codeCorrect==0){
//          DigestUtils.md5Digest("ass".getBytes(StandardCharsets.UTF_8)); // 加密
//          DigestUtils.md5DigestAsHex("ass".getBytes(StandardCharsets.UTF_8)); // 验证
            String s = DigestUtils.md5DigestAsHex(password.getBytes(StandardCharsets.UTF_8));// 加密
            System.out.println(password+"加密后是："+s);
            User user=new User();
            user.setEmail(email);
            user.setPassword(s);
            userDao.save(user);
            return true;
        }else {
            return false;
        }

    }


    @Override
    public boolean isRegister(String email) {
        User user=new User();
        user.setEmail(email);
        List<User> all = userDao.findAll(Example.of(user));
        if(CollectionUtils.isEmpty(all)){
            return false;
        }
        return true;
    }

    @Override
    public String getEmail(String token) {
        User user=new User();
        user.setToken(token);
        List<User> all = userDao.findAll(Example.of(user));

        if(!CollectionUtils.isEmpty(all)&&all.size()==1){
            return all.get(0).getEmail();
        }
        return null;
    }

    @Override
    public String login(String email, String password,HttpServletResponse response) {
        if(StringUtils.isBlank(email)||StringUtils.isBlank(password)){
            return null;
        }
        User user=new User();
        user.setEmail(email);
        List<User> all = userDao.findAll(Example.of(user));
        String s = DigestUtils.md5DigestAsHex(password.getBytes(StandardCharsets.UTF_8));
        System.out.println(password+"加密后是："+s);
        if(!CollectionUtils.isEmpty(all)&&all.size()==1){
            System.out.println("数据库密码是："+all.get(0).getPassword());
            if(all.get(0).getPassword().equals(s)){
                String token= UUID.randomUUID().toString();
                user.setId(all.get(0).getId());
                user.setToken(token);
                //将token写入cookie
                Cookie cookie=new Cookie("token",token);
                // tomcat下多应用共享
                cookie.setPath("/");
                cookie.setMaxAge(30*60);
                response.addCookie(cookie);
                //保存token
                user.setPassword(password);
                userDao.save(user);
                return all.get(0).getEmail();
            }
        }
        return null;
    }
}
